/**
 * 鱼钩类
 */
class FishHook extends eui.Component
{
    private line: egret.Shape;
    private isCatch: boolean;
    public zeroX: number;
    public zeroY: number;

    public h: number = 600;
    private r: number = 70;
    private go: boolean = false;
    private speed: number = 15;

    private tween:egret.Tween;

    //是否发事件
    private isNeedSendEvent = true;

    //抓到的鱼的集合
    public catchFishArr:Array<Fish> = [];


    public constructor()
    {
        super();
    }

    protected childrenCreated(): void
    {
        super.childrenCreated();
        this.zeroX = this.x;
        this.zeroY = this.y;
        this.line = new egret.Shape();
        this.parent.addChild(this.line);
    }

    private getTime(dis: number, speed): number
    {
        let a: number = dis * 20 / (this.speed + speed);
        return a - a % 100;
    }

    private getDistance(x: number, y: number): number
    {
        var a: number = Math.abs(this.x - x);
        var b: number = Math.abs(this.y - y);
        var c: number = Math.pow(a, 2) + Math.pow(b, 2);
        return Math.sqrt(c);
    }

    /**点击伸出去抓*/
    public MoveDown(): void
    {
        this.line.visible = true;
        var tarY: number = this.h;
        this.tween = egret.Tween.get(this, { onChange: this.changeline, onChangeObj: this });
        this.tween.to({
            y : tarY
        },300)
        .call(()=>{
            if(this.isNeedSendEvent)
            {
                this.isNeedSendEvent = false;
                GameUtil.dispatchEvent(EventConst.CatchStart);
            }
        })
    }
    
    public goBack():void
    {
        // this.tween = egret.Tween.get(this, { onChange: this.changeline, onChangeObj: this });
        var time: number = this.getTime(200, 0);
        // this.zeroY += 500;
        this.tween.to({ x: this.zeroX-this.width+16, y: this.zeroY }, time);
    }

    public async goBack2(time){
        // this.zeroY -= 500;
        this.tween = null;
        let logic:MainUI = UIManager.instance().getLogicByName(UIConst.MAIN_UI) as MainUI;
        this.tween = egret.Tween.get(this, { onChange: this.changeline, onChangeObj: this });
        this.tween.to({ x: this.zeroX-this.width+16, y: this.zeroY }, time)
        .call(()=>{
            egret.Tween.get(logic.main_group).to({
                y:0
            }, 200)
        })
        .wait(200).call(()=>{
            FishpondUI.instance().FinishThisGame();
        });
    }

    public changeline(): void
    {
        this.line.graphics.clear();
        this.line.graphics.lineStyle(2, 0x4D4D4D);
        this.line.graphics.moveTo(this.zeroX, this.zeroY);
        this.line.graphics.lineTo(this.x+this.width-6, this.y);
        this.line.graphics.endFill();
    }

    public setZeroPoint(x, y)
    {
        this.zeroX = x;
        this.zeroY = y;
    }

    //抓到鱼
    public catchFish(Fish:Fish)
    {
        this.catchFishArr.push(Fish);
        Fish.afterCollision();
        this.addChild(Fish);
        Fish.y = this.height-3;
        Fish.x = (this.width >>1) - 2;
    }

    //清除数据
    public clear()
    {
        this.zeroX = this.zeroY = 0;
        this.line.visible = false;
        this.isNeedSendEvent = true;
        this.tween = null;
        for(let v of this.catchFishArr)
        {
            v.destory();
            v = null;
        }
        this.catchFishArr = [];
    }

    //获取抓到的鱼的数据
    public getFishData()
    {
        let arr = [];
        for(let v of this.catchFishArr)
        {
            arr.push(v.getFishData());
        }
        return arr;
    }

    public destory()
    {
        this.zeroX = this.zeroY = null;
        this.line = null;
        this.tween = null;
        this.isNeedSendEvent = null;
        for(let v of this.catchFishArr)
        {
            v.destory();
            v = null;
        }
    }
}

window['FishHook'] = FishHook;